import { CreepRoles } from '../structures/spawn/types'
import { AllowCrossRuleFunc, CrossRules } from './types'

/**
 * 默认的对穿规则
 *
 * 当自己正在站定工作，并且请求对穿的和自己是相同角色时拒绝对穿
 *
 * @param creep 被对穿的 creep
 * @param requireCreep 发起对穿的 creep
 */
const defaultRule: AllowCrossRuleFunc = (creep, requireCreep) =>
    !(creep.memory.stand && requireCreep.memory.role === creep.memory.role)

/**
 * 站定时不允许对穿
 * @param creep 被对穿的 creep
 */
const noCrossWithStanding: AllowCrossRuleFunc = (creep) => !creep.memory.stand

/**
 * 通用对穿规则
 */
const generalCrossRule: AllowCrossRuleFunc = (creep, requireCreep) => {
    return (
        !creep.memory.stand ||
        requireCreep.memory.role === CreepRoles.defender ||
        requireCreep.memory.role === CreepRoles.transporter ||
        requireCreep.memory.role === CreepRoles.upgradeCarrier ||
        requireCreep.memory.role === CreepRoles.remoteCarrier ||
        requireCreep.memory.role === CreepRoles.teamMember
    )
}

/**
 * 升级爬对穿规则
 */
const upgradeCrossRule: AllowCrossRuleFunc = (creep, requireCreep) => {
    if (!creep.memory.stand) return true

    if (requireCreep.memory.role === CreepRoles.upgrader || requireCreep.memory.role === CreepRoles.upgradeCarrier)
        return false

    return true
}

/**
 * 对穿规则合集
 *
 * 返回值代表了 creep 是否允许 requireCreep 对穿
 */
const crossRules: CrossRules = {
    // 【默认规则】自己在工作时有同角色 creep 发起对穿则拒绝对穿
    default: defaultRule,

    // 填充单位无论什么时候都会允许对穿，因为其不会长时间停在一个位置上工作
    [CreepRoles.transporter]: () => true,
    [CreepRoles.upgradeCarrier]: () => true,
    [CreepRoles.scout]: () => true,

    // 采集单位在采集能量时不允许对穿
    [CreepRoles.harvester]: noCrossWithStanding,
    // 建造爬
    [CreepRoles.builder]: generalCrossRule,
    // 升级爬
    [CreepRoles.upgrader]: upgradeCrossRule,
}

export default crossRules
